<?php

namespace App\Libraries\DingTalk;

class DingTalkWorkflow extends DingTalkBase
{
    public function getOptions(array $json = []): array
    {
        $options = [];
        $options['headers'] = [
            'Content-Type' => 'application/json; charset=UTF-8',
            'x-acs-dingtalk-access-token' => $this->getAccessToken(),
        ];

        if (! empty($json)) {
            $options['json'] = $json;
        }

        return $options;
    }

    /**
     * 获取审批实例
     *
     * @param  string $processInstanceId 审批实例ID
     * @return array
     */
    public function getProcessInstances(string $processInstanceId): array
    {
        $options = $this->getOptions();

        return $this->requestByGet(
            'https://api.dingtalk.com/v1.0/workflow/processInstances?processInstanceId=' . $processInstanceId,
            $options
        );
    }

    /**
     * 获取审批ID列表
     *
     * @param  [type]  $startTime  开始时间 毫秒级时间戳
     * @param  [type]  $endTime    结束事件 毫秒级时间戳
     * @param  int   $nextToken  分页游标
     * @param  int   $maxResults 分页参数，每页大小，最多传20
     * @param  array $userIds    发起人userId列表，最大列表长度为10
     * @param  array $statuses   流程实例状态: RUNNING：审批中, TERMINATED：被终止, COMPLETED：完成, CANCELED：取消
     * @return array
     */
    public function getProcessInstanceIds(int $startTime, int $endTime, int $nextToken = 0, int $maxResults = 20, $userIds = [], $statuses = []): array
    {
        $options = $this->getOptions([
            'processCode' => 'PROC-EFB64623-4035-4820-91F5-EBDBA23631E8',
            'startTime' => $startTime,
            'endTime' => $endTime,
            'nextToken' => $nextToken,
            'maxResults' => $maxResults,
            'userIds' => $userIds,
            'statuses' => $statuses,
        ]);

        return $this->requestByPost(
            'https://api.dingtalk.com/v1.0/workflow/processes/instanceIds/query',
            $options
        );
    }

    /**
     * 创建审批实例
     *
     * @param  array $params 表单数据
     * @return array
     */
    public function addProcessInstances(array $params): array
    {
        $options = $this->getOptions($params);

        return $this->requestByPost(
            'https://api.dingtalk.com/v1.0/workflow/processInstances',
            $options
        );
    }

    /**
     * 撤销审批
     *
     * @param  string $processInstanceId 审批实例ID
     * @param  bool   $isSystem          是否通过系统操作 true: 由系统直接终止, false: 由指定的操作者终止（需要传发起人才能撤销）
     * @param  string $remark            终止说明
     * @param  string $operatingUserId   操作人的userId
     * @return array
     */
    public function terminateProcessInstances(string $processInstanceId, boolean $isSystem = false, string $remark = '', string $operatingUserId = ''): array
    {
        $options = $this->getOptions([
            'processInstanceId' => $processInstanceId,
            'isSystem' => $isSystem,
            'remark' => $remark,
            'operatingUserId' => $operatingUserId,
        ]);

        return $this->requestByPost(
            'https://api.dingtalk.com/v1.0/workflow/processInstances/terminate',
            $options
        );
    }

    /**
     * 同意或拒绝审批
     *
     * @param string $processInstanceId 审批实例ID
     * @param string $result            审批操作 agree: 同意, refuse: 拒绝
     * @param string $actionerUserId    操作人userId
     * @param string $taskId            任务ID
     * @param string $remark            审批意见
     * @return [type]                    [description]
     */
    public function executeProcessInstances(string $processInstanceId, string $result, string $actionerUserId, string $taskId, string $remark = ''): array
    {
        $options = $this->getOptions([
            'processInstanceId' => $processInstanceId,
            'remark' => $remark,
            'result' => $result,
            'actionerUserId' => $actionerUserId,
            'taskId' => $taskId,
        ]);

        return $this->requestByPost(
            'https://api.dingtalk.com/v1.0/workflow/processInstances/execute',
            $options
        );
    }
}
